package com.toommi.refuse.config;

import com.toommi.refuse.entity.*;
import com.toommi.refuse.mapper.SysPermissionMapper;
import com.toommi.refuse.mapper.SysRoleMapper;
import com.toommi.refuse.mapper.SysRolePermissionMapper;
import com.toommi.refuse.service.SysUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j

public class MyShiroRealmService extends AuthorizingRealm {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysRoleMapper roleMapper;
    @Resource
    private SysPermissionMapper permissionMapper;
    @Resource
    private SysRolePermissionMapper rolePermissionMapper;


    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        String username = (String) token.getPrincipal();
        log.debug("token带来的数据：  " + username);

//		String passwordDataSource = userService.findPass(username);
//		log.debug("从数据库中查询到的数据密码：{}",passwordDataSource);
        SysUser user = sysUserService.findUserDetail(username);
        log.debug("user：{}", user);

        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
                user, //用户对象--数据库
                user.getPassword(), //密码--数据库
                ByteSource.Util.bytes(""),
                getName()  //realm name
        );
        return simpleAuthenticationInfo;
    }


    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

        String userName = ((SysUser) getAvailablePrincipal(principals)).getUserName();
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        SysUser sysUser = sysUserService.findUserDetail(userName);

        //用户角色
        SysRole sysRole = roleMapper.selectByPrimaryKey(sysUser.getSysRoleId());
        Set<String> roleIds = new HashSet<String>();
        roleIds.add(sysRole.getRoleName());
        //角色对应权限

        SysRolePermissionExample example1 = new SysRolePermissionExample();
        example1.createCriteria().andSysRoleIdEqualTo(sysRole.getId());
        List<SysRolePermission> permissionList = rolePermissionMapper.selectByExample(example1);

        if (permissionList != null && !permissionList.isEmpty()) {
            Set<String> handleMappings = new HashSet<String>();
            for (SysRolePermission sysRolePermission : permissionList) {
                Long permissionId = sysRolePermission.getSysPermissionId();
                SysPermission permission = permissionMapper.selectByPrimaryKey(permissionId);
                handleMappings.add(permission.getPerssionCode());
            }
            info.setStringPermissions(handleMappings);
        }
        info.setRoles(roleIds);

        return info;
    }
}
